<?php
global $_W, $_GPC;
$uid = $_GPC['uid'];
$weid = $_W['uniacid'];
$type = empty($_GPC['type']) ? 1 : $_GPC['type'];//1：周  2：总
if (empty($uid)) {
    return $this->getresult(1, '请先登录', array('code' => 3001, 'msg' => '请先登录'));
}

/***校验token开始***/
if ($uid) {
    $token = $_GPC['token'];
    if (empty($token)) {
        return $this->getresult(1, '请重新登录', ['code' => 4001, 'msg' => '请重新登录']);
    }
    $token_result = $this->checktoken($uid, $token);
    if ($token_result == 1002) {
        return $this->getresult(1, '请重新登录', ['code' => 4002, 'msg' => '请重新登录']);
    } elseif ($token_result == 1003) {
        return $this->getresult(1, '请重新登录', ['code' => 4003, 'msg' => '请重新登录']);
    } elseif ($token_result == 1001) {
        //  更新token
        pdo_update($this->t_user_token, array('createtime' => time()), array('uid' => $uid, 'token' => $token, 'weid' => $_W['uniacid']));
    }
}
/***校验token结束***/
if ($type == 1) {
    $beginWeek = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y"));
    $endWeek = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y"));
    $tparams = array(':weid' => $weid, ':start' => $beginWeek, ':end' => $endWeek, ':status' => 1);
    $totalAll = pdo_fetchall("select sum(us.num) as totalStar,us.uid,u.headimg,u.nickname,u.myheaderimg from " . tablename('goouc_class_user_star') . " us LEFT JOIN " . tablename('goouc_class_user') . " u ON us.uid = u.id  where us.weid = :weid AND us.status = :status AND us.createtime >= :start AND us.createtime <= :end  group by us.uid order by totalStar desc limit 0,23", $tparams);
    $params = array(':weid' => $weid, ':uid' => $uid, ':start' => $beginWeek, ':end' => $endWeek, ':status' => 1);
    $totalStar = pdo_fetchall("select sum(us.num) as totalStar,us.uid,u.nickname,u.headimg,u.myheaderimg from " . tablename('goouc_class_user_star') . " us LEFT JOIN " . tablename('goouc_class_user') . " u ON us.uid = u.id  where us.weid = :weid AND us.status = :status AND us.uid = :uid AND us.createtime >= :start AND us.createtime <= :end ", $params);

} elseif ($type == 2) {
    $tparams = array(':weid' => $weid, ':status' => 1);
    $totalAll = pdo_fetchall("select sum(us.num) as totalStar,us.uid,u.headimg,u.nickname,u.myheaderimg from " . tablename('goouc_class_user_star') . " us LEFT JOIN " . tablename('goouc_class_user') . " u ON us.uid = u.id  where us.weid = :weid AND us.status = :status group by us.uid order by totalStar desc limit 0,23", $tparams);
    $params = array(':weid' => $weid, ':uid' => $uid, ':status' => 1);
    $totalStar = pdo_fetchall("select sum(us.num) as totalStar,us.uid,u.nickname,u.headimg,u.myheaderimg from " . tablename('goouc_class_user_star') . " us LEFT JOIN " . tablename('goouc_class_user') . " u ON us.uid = u.id  where us.weid = :weid AND us.uid = :uid AND us.status = :status ", $params);
}
//如果新用户没有数据要显示
if (empty($totalStar[0]['uid'])) {
    $userInfo = pdo_get('goouc_class_user', array('id' => $uid), array('headimg', 'myheaderimg', 'nickname'));
    if (!$userInfo['myheaderimg']) {
        $totalStar[0]['headimg'] = $userInfo['headimg'];
    } else {
        $totalStar[0]['headimg'] = $userInfo['myheaderimg'];
    }

    $totalStar[0]['nickname'] = $userInfo['nickname'];
    $totalStar[0]['totalStar'] = 0;
    $totalStar[0]['uid'] = $uid;
}

$three = [];
$lastList = [];
foreach ($totalAll as $k => &$v) {
    $idList[] = $v['uid'];
    $v['sort'] = $k + 1;
    if ($k < 3) {
        $three[] = $v;
        unset($totalAll[$k]);
    }
    $v['headimg'] = $v['myheaderimg'];
    $lastList[] = $v;
}

//判断是否上榜
if (is_array($idList)) {
    if (in_array($totalStar[0]['uid'], $idList)) {
        $totalStar[0]['status'] = 1;//上榜
        $totalStar[0]['sort'] = array_search($totalStar[0]['uid'], $idList) + 1;
    } else {
        $totalStar[0]['status'] = 2;//未上榜
    }
}

$data = [
    'three' => $three,//前三
    'list' => $lastList,//前20
    'totalStar' => $totalStar[0]//该会员总星星
];
return $this->getresult(0, '星际排行', ['code' => 200, 'listdata' => $data]);